package com.wss.module.wan.common;

import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseApplication;
import com.wss.common.net.Api;
import com.wss.common.profile.ProfileManager;
import com.wss.module.wan.BuildConfig;
import com.wss.module.wan.profile.ProfileFactory;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.model.HttpHeaders;
import com.zhouyou.http.model.HttpParams;
import com.zhouyou.http.utils.HttpLog;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

public class WanApplication extends BaseApplication {
    @Override
    public void onInitialize() {
        super.onInitialize();
        EasyHttp.init(this);

        ProfileManager.inst.factory(new ProfileFactory());
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        //设置请求参数
        HttpParams params = new HttpParams();
        //        params.put("appId", AppConstant.APPID);
        EasyHttp.getInstance()
                .debug("RxEasyHttp", BuildConfig.DEBUG)
                .setReadTimeOut(60 * 1000)
                .setWriteTimeOut(60 * 1000)
                .setConnectTimeout(60 * 1000)
                .setRetryCount(3)//默认网络不好自动重试3次
                .setRetryDelay(500)//每次延时500ms重试
                .setRetryIncreaseDelay(500)//每次延时叠加500ms
                .setBaseUrl(Api.BASE_URL)
                .setCacheDiskConverter(new SerializableDiskConverter())//默认缓存使用序列化转化
                .setCacheMaxSize(50 * 1024 * 1024)//设置缓存大小为50M
                .setCacheVersion(1)//缓存版本为1
                .setHostnameVerifier(new UnSafeHostnameVerifier(Api.BASE_URL))//全局访问规则
                .setCertificates()//信任所有证书
                //    .addConverterFactory(GsonConverterFactory.create(gson))//本框架没有采用Retrofit的Gson转化，所以不用配置
                .addCommonHeaders(headers)//设置全局公共头
                .addCommonParams(params);//设置全局公共参数
    }

    public class UnSafeHostnameVerifier implements HostnameVerifier {
        private String host;
        public UnSafeHostnameVerifier(String host) {

            this.host = host;
        }

        @Override
        public boolean verify(String hostname, SSLSession sslSession) {
            if (this.host == null || "".equals(this.host) |!this.host.contains(hostname))  return false;
            return true;
        }
    }

}
